home *** CD-ROM | disk | FTP | other *** search
/ CDROM of CDROMs 1994 May / The CD-ROM of CD-ROMs - The Consumer's Guide to CD-ROMs - Walnut Creek May 1994.iso / samples / unix / part2 < prev    next >
Encoding:
Internet Message Format  |  1994-04-12  |  31.1 KB

  1. From: tmatimar@isgtec.com (Ted M A Timar)
  2. Newsgroups: comp.unix.questions,comp.unix.shell,news.answers,comp.answers
  3. Subject: Unix - Frequently Asked Questions (2/7) [Frequent posting]
  4.  
  5. Archive-name: unix-faq/faq/part2
  6. Version: $Id: part2,v 2.2 1993/03/18 23:06:21 tmatimar Exp $
  7.  
  8. These seven articles contain the answers to some Frequently Asked
  9. Questions often seen in comp.unix.questions and comp.unix.shell.
  10. Please don't ask these questions again, they've been answered plenty
  11. of times already - and please don't flame someone just because they may
  12. not have read this particular posting.  Thank you.
  13.  
  14. Many FAQs, including this one, are available on the archive site
  15. rtfm.mit.edu (18.172.1.27) in the directory pub/usenet/news.answers.
  16. The name under which a FAQ is archived appears in the "Archive-Name:"
  17. line at the top of the article.  This FAQ is archived as
  18. "unix-faq/faq/part[1-7]".
  19.  
  20. These articles are divided approximately as follows:
  21.  
  22.       1.*) General questions.
  23.       2.*) Relatively basic questions, likely to be asked by beginners.
  24.       3.*) Intermediate questions.
  25.       4.*) Advanced questions, likely to be asked by people who thought
  26.        they already knew all of the answers.
  27.       5.*) Questions pertaining to the various shells, and the differences.
  28.       6.*) An overview of Unix variants.
  29.       7.*) An comparison of configuration management systems (RCS, SCCS).
  30.  
  31. This article includes answers to:
  32.  
  33.       2.1)  How do I remove a file whose name begins with a "-" ?
  34.       2.2)  How do I remove a file with funny characters in the filename ?
  35.       2.3)  How do I get a recursive directory listing?
  36.       2.4)  How do I get the current directory into my prompt?
  37.       2.5)  How do I read characters from the terminal in a shell script?
  38.       2.6)  How do I rename "*.foo" to "*.bar", or change file names
  39.               to lowercase?
  40.       2.7)  Why do I get [some strange error message] when I
  41.               "rsh host command" ?
  42.       2.8)  How do I {set an environment variable, change directory} inside a
  43.               program or shell script and have that change affect my
  44.               current shell?
  45.       2.9)  How do I redirect stdout and stderr separately in csh?
  46.       2.10) How do I tell inside .cshrc if I'm a login shell?
  47.       2.11) How do I construct a shell glob-pattern that matches all files
  48.             except "." and ".." ?
  49.       2.12) How do I find the last argument in a Bourne shell script?
  50.       2.13) What's wrong with having '.' in your $PATH ?
  51.  
  52. If you're looking for the answer to, say, question 2.5, and want to skip
  53. everything else, you can search ahead for the regular expression "^2.5)".
  54.  
  55. While these are all legitimate questions, they seem to crop up in
  56. comp.unix.questions or comp.unix.shell on an annual basis, usually
  57. followed by plenty of replies (only some of which are correct) and then
  58. a period of griping about how the same questions keep coming up.  You
  59. may also like to read the monthly article "Answers to Frequently Asked
  60. Questions" in the newsgroup "news.announce.newusers", which will tell
  61. you what "UNIX" stands for.
  62.  
  63. With the variety of Unix systems in the world, it's hard to guarantee
  64. that these answers will work everywhere.  Read your local manual pages
  65. before trying anything suggested here.  If you have suggestions or
  66. corrections for any of these answers, please send them to to
  67. tmatimar@isgtec.com.
  68.  
  69. ----------------------------------------------------------------------
  70.  
  71. Subject: How do I remove a file whose name begins with a "-" ?
  72. Date: Thu Mar 18 17:16:55 EST 1993
  73.  
  74. 2.1)  How do I remove a file whose name begins with a "-" ?
  75.  
  76.       Figure out some way to name the file so that it doesn't begin
  77.       with a dash.  The simplest answer is to use
  78.  
  79.         rm ./-filename
  80.  
  81.       (assuming "-filename" is in the current directory, of course.)
  82.       This method of avoiding the interpretation of the "-" works with
  83.       other commands too.
  84.  
  85.       Many commands, particularly those that have been written to use
  86.       the "getopt(3)" argument parsing routine, accept a "--" argument
  87.       which means "this is the last option, anything after this is not
  88.       an option", so your version of rm might handle "rm -- -filename".
  89.       Some versions of rm that don't use getopt() treat a single "-"
  90.       in the same way, so you can also try "rm - -filename".
  91.  
  92. ------------------------------
  93.  
  94. Subject: How do I remove a file with funny characters in the filename ?
  95. Date: Thu Mar 18 17:16:55 EST 1993
  96.  
  97. 2.2)  How do I remove a file with funny characters in the filename ?
  98.  
  99.       If the 'funny character' is a '/', skip to the last part of this
  100.       answer.  If the funny character is something else, such as a ' '
  101.       or control character or character with the 8th bit set, keep reading.
  102.  
  103.       The classic answers are
  104.  
  105.     rm -i some*pattern*that*matches*only*the*file*you*want
  106.  
  107.     which asks you whether you want to remove each file matching
  108.     the indicated pattern;  depending on your shell, this may not
  109.     work if the filename has a character with the 8th bit set (the
  110.     shell may strip that off);
  111.  
  112.       and
  113.  
  114.     rm -ri .
  115.  
  116.     which asks you whether to remove each file in the directory.
  117.     Answer "y" to the problem file and "n" to everything else.
  118.     Unfortunately this doesn't work with many versions of rm.  Also
  119.     unfortunately, this will walk through every subdirectory of ".",
  120.     so you might want to "chmod a-x" those directories temporarily
  121.     to make them unsearchable.
  122.  
  123.     Always take a deep breath and think about what you're doing and
  124.     double check what you typed when you use rm's "-r" flag or a
  125.     wildcard on the command line;
  126.  
  127.       and
  128.  
  129.     find . -type f ... -ok rm '{}' \;
  130.  
  131.       where "..." is a group of predicates that uniquely identify the
  132.       file.  One possibility is to figure out the inode number of the
  133.       problem file (use "ls -i .") and then use
  134.  
  135.     find . -inum 12345 -ok rm '{}' \;
  136.  
  137.       or
  138.     find . -inum 12345 -ok mv '{}' new-file-name \;
  139.     
  140.       "-ok" is a safety check - it will prompt you for confirmation of
  141.       the command it's about to execute.  You can use "-exec" instead
  142.       to avoid the prompting, if you want to live dangerously, or if
  143.       you suspect that the filename may contain a funny character
  144.       sequence that will mess up your screen when printed.
  145.  
  146.       What if the filename has a '/' in it?
  147.  
  148.       These files really are special cases, and can only be created by
  149.       buggy kernel code (typically by implementations of NFS that don't
  150.       filter out illegal characters in file names from remote
  151.       machines.)  The first thing to do is to try to understand exactly
  152.       why this problem is so strange.
  153.  
  154.       Recall that Unix directories are simply pairs of filenames and
  155.       inode numbers.  A directory essentially contains information
  156.       like this:
  157.  
  158.     filename  inode
  159.  
  160.     file1      12345
  161.     file2.c      12349
  162.     file3     12347
  163.  
  164.       Theoretically, '/' and '\0' are the only two characters that
  165.       cannot appear in a filename - '/' because it's used to separate
  166.       directories and files, and '\0' because it terminates a filename.
  167.  
  168.       Unfortunately some implementations of NFS will blithely create
  169.       filenames with embedded slashes in response to requests from
  170.       remote machines.  For instance, this could happen when someone on
  171.       a Mac or other non-Unix machine decides to create a remote NFS
  172.       file on your Unix machine with the date in the filename.  Your
  173.       Unix directory then has this in it:
  174.  
  175.     filename  inode
  176.  
  177.     91/02/07  12357
  178.  
  179.       No amount of messing around with 'find' or 'rm' as described
  180.       above will delete this file, since those utilities and all other
  181.       Unix programs, are forced to interpret the '/' in the normal way.
  182.  
  183.       Any ordinary program will eventually try to do
  184.       unlink("91/02/07"), which as far as the kernel is concerned means
  185.       "unlink the file 07 in the subdirectory 02 of directory 91", but
  186.       that's not what we have - we have a *FILE* named "91/02/07" in
  187.       the current directory.  This is a subtle but crucial distinction.
  188.  
  189.       What can you do in this case?  The first thing to try is to
  190.       return to the Mac that created this crummy entry, and see if you
  191.       can convince it and your local NFS daemon to rename the file to
  192.       something without slashes.
  193.  
  194.       If that doesn't work or isn't possible, you'll need help from
  195.       your system manager, who will have to try the one of the
  196.       following.  Use "ls -i" to find the inode number of this bogus
  197.       file, then unmount the file system and use "clri" to clear the
  198.       inode, and "fsck" the file system with your fingers crossed.
  199.       This destroys the information in the file.  If you want to keep
  200.       it, you can try:
  201.  
  202.     create a new directory in the same parent directory as the one
  203.     containing the bad file name;
  204.  
  205.     move everything you can (i.e. everything but the file with the
  206.     bad name) from the old directory to the new one;
  207.  
  208.     do "ls -id" on the directory containing the file with the bad
  209.     name to get its inumber;
  210.  
  211.     umount the file system;
  212.  
  213.     "clri" the directory containing the file with the bad name;
  214.  
  215.     "fsck" the file system.
  216.  
  217.       Then, to find the file,
  218.  
  219.     remount the file system;
  220.  
  221.     rename the directory you created to have the name of the old
  222.     directory (since the old directory should have been blown away
  223.     by "fsck")
  224.  
  225.     move the file out of "lost+found" into the directory with a
  226.     better name.
  227.  
  228.       Alternatively, you can patch the directory the hard way by
  229.       crawling around in the raw file system.  Use "fsdb", if you
  230.       have it.
  231.  
  232. ------------------------------
  233.  
  234. Subject: How do I get a recursive directory listing?
  235. Date: Thu Mar 18 17:16:55 EST 1993
  236.  
  237. 2.3)  How do I get a recursive directory listing?
  238.  
  239.       One of the following may do what you want:
  240.  
  241.     ls -R             (not all versions of "ls" have -R)
  242.     find . -print        (should work everywhere)
  243.     du -a .            (shows you both the name and size)
  244.  
  245.       If you're looking for a wildcard pattern that will match all ".c"
  246.       files in this directory and below, you won't find one, but you
  247.       can use
  248.  
  249.     % some-command `find . -name '*.c' -print`
  250.  
  251.       "find" is a powerful program.  Learn about it.
  252.  
  253. ------------------------------
  254.  
  255. Subject: How do I get the current directory into my prompt?
  256. Date: Thu Mar 18 17:16:55 EST 1993
  257.  
  258. 2.4)  How do I get the current directory into my prompt?
  259.  
  260.       It depends which shell you are using.  It's easy with some
  261.       shells, hard or impossible with others.
  262.  
  263.       C Shell (csh):
  264.     Put this in your .cshrc - customize the prompt variable the
  265.     way you want.
  266.  
  267.         alias setprompt 'set prompt="${cwd}% "'
  268.         setprompt        # to set the initial prompt
  269.         alias cd 'chdir \!* && setprompt'
  270.     
  271.     If you use pushd and popd, you'll also need
  272.  
  273.         alias pushd 'pushd \!* && setprompt'
  274.         alias popd  'popd  \!* && setprompt'
  275.  
  276.     Some C shells don't keep a $cwd variable - you can use
  277.     `pwd` instead.
  278.  
  279.     If you just want the last component of the current directory
  280.     in your prompt ("mail% " instead of "/usr/spool/mail% ")
  281.     you can use
  282.  
  283.         alias setprompt 'set prompt="$cwd:t% "'
  284.     
  285.     Some older csh's get the meaning of && and || reversed.
  286.     Try doing:
  287.  
  288.         false && echo bug
  289.  
  290.     If it prints "bug", you need to switch && and || (and get
  291.     a better version of csh.)
  292.  
  293.       Bourne Shell (sh):
  294.  
  295.     If you have a newer version of the Bourne Shell (SVR2 or newer)
  296.     you can use a shell function to make your own command, "xcd" say:
  297.  
  298.         xcd() { cd $* ; PS1="`pwd` $ "; }
  299.  
  300.     If you have an older Bourne shell, it's complicated but not
  301.     impossible.  Here's one way.  Add this to your .profile file:
  302.  
  303.         LOGIN_SHELL=$$ export LOGIN_SHELL
  304.         CMDFILE=/tmp/cd.$$ export CMDFILE
  305.         # 16 is SIGURG, pick a signal that's not likely to be used
  306.         PROMPTSIG=16 export PROMPTSIG
  307.         trap '. $CMDFILE' $PROMPTSIG
  308.  
  309.     and then put this executable script (without the indentation!),
  310.     let's call it "xcd", somewhere in your PATH
  311.  
  312.         : xcd directory - change directory and set prompt
  313.         : by signalling the login shell to read a command file
  314.         cat >${CMDFILE?"not set"} <<EOF
  315.         cd $1
  316.         PS1="\`pwd\`$ "
  317.         EOF
  318.         kill -${PROMPTSIG?"not set"} ${LOGIN_SHELL?"not set"}
  319.  
  320.     Now change directories with "xcd /some/dir".
  321.  
  322.       Korn Shell (ksh):
  323.  
  324.     Put this in your .profile file:
  325.         PS1='$PWD $ '
  326.     
  327.     If you just want the last component of the directory, use
  328.         PS1='${PWD##*/} $ '
  329.  
  330.       T C shell (tcsh)
  331.  
  332.     Tcsh is a popular enhanced version of csh with some extra
  333.     builtin variables (and many other features):
  334.  
  335.         %~        the current directory, using ~ for $HOME
  336.         %/        the full pathname of the current directory
  337.         %c or %.    the trailing component of the current directory
  338.  
  339.     so you can do
  340.  
  341.         set prompt='%~ '
  342.  
  343.       BASH (FSF's "Bourne Again SHell")
  344.     
  345.     \w in $PS1 gives the full pathname of the current directory,
  346.     with ~ expansion for $HOME;  \W gives the basename of
  347.     the current directory.  So, in addition to the above sh and
  348.     ksh solutions, you could use
  349.  
  350.         PS1='\w $ '    
  351.     or
  352.         PS1='\W $ '
  353.  
  354. ------------------------------
  355.  
  356. Subject: How do I read characters from the terminal in a shell script?
  357. Date: Thu Mar 18 17:16:55 EST 1993
  358.  
  359. 2.5)  How do I read characters from the terminal in a shell script?
  360.  
  361.       In sh, use read.  It is most common to use a loop like
  362.  
  363.         while read line
  364.         do
  365.             ...
  366.         done
  367.  
  368.       In csh, use $< like this:
  369.     
  370.         while ( 1 )
  371.         set line = "$<"
  372.         if ( "$line" == "" ) break
  373.         ...
  374.         end
  375.  
  376.       Unfortunately csh has no way of distinguishing between a blank
  377.       line and an end-of-file.
  378.  
  379.       If you're using sh and want to read a *single* character from the
  380.       terminal, you can try something like
  381.  
  382.         echo -n "Enter a character: "
  383.         stty cbreak        # or  stty raw
  384.         readchar=`dd if=/dev/tty bs=1 count=1 2>/dev/null`
  385.         stty -cbreak
  386.  
  387.         echo "Thank you for typing a $readchar ."
  388.  
  389. ------------------------------
  390.  
  391. Subject: How do I rename "*.foo" to "*.bar", or change file names to lowercase?
  392. Date: Thu Mar 18 17:16:55 EST 1993
  393.  
  394. 2.6)  How do I rename "*.foo" to "*.bar", or change file names to lowercase?
  395.     
  396.       Why doesn't "mv *.foo *.bar" work?  Think about how the shell
  397.       expands wildcards.   "*.foo" and "*.bar" are expanded before the
  398.       mv command ever sees the arguments.  Depending on your shell,
  399.       this can fail in a couple of ways.  CSH prints "No match."
  400.       because it can't match "*.bar".  SH executes "mv a.foo b.foo
  401.       c.foo *.bar", which will only succeed if you happen to have a
  402.       single directory named "*.bar", which is very unlikely and almost
  403.       certainly not what you had in mind.
  404.  
  405.       Depending on your shell, you can do it with a loop to "mv" each
  406.       file individually.  If your system has "basename", you can use:
  407.  
  408.       C Shell:
  409.     foreach f ( *.foo )
  410.         set base=`basename $f .foo`
  411.         mv $f $base.bar
  412.     end
  413.  
  414.       Bourne Shell:
  415.     for f in *.foo; do
  416.         base=`basename $f .foo`
  417.         mv $f $base.bar
  418.     done
  419.  
  420.       Some shells have their own variable substitution features, so
  421.       instead of using "basename", you can use simpler loops like:
  422.  
  423.       C Shell:
  424.  
  425.     foreach f ( *.foo )
  426.         mv $f $f:r.bar
  427.     end
  428.  
  429.       Korn Shell:
  430.  
  431.     for f in *.foo; do
  432.         mv $f ${f%foo}bar
  433.     done
  434.  
  435.       If you don't have "basename" or want to do something like
  436.       renaming foo.* to bar.*, you can use something like "sed" to
  437.       strip apart the original file name in other ways, but the general
  438.       looping idea is the same.  You can also convert file names into
  439.       "mv" commands with 'sed', and hand the commands off to "sh" for
  440.       execution.  Try
  441.  
  442.     ls -d *.foo | sed -e 's/.*/mv & &/' -e 's/foo$/bar/' | sh
  443.  
  444.       A program by Vladimir Lanin called "mmv" that does this job
  445.       nicely was posted to comp.sources.unix (Volume 21, issues 87 and
  446.       88) in April 1990.  It lets you use
  447.  
  448.     mmv '*.foo' '=1.bar'
  449.  
  450.       Shell loops like the above can also be used to translate file
  451.       names from upper to lower case or vice versa.  You could use
  452.       something like this to rename uppercase files to lowercase:
  453.  
  454.     C Shell:
  455.         foreach f ( * )
  456.         mv $f `echo $f | tr '[A-Z]' '[a-z]'`
  457.         end
  458.     Bourne Shell:
  459.         for f in *; do
  460.         mv $f `echo $f | tr '[A-Z]' '[a-z]'`
  461.         done
  462.     Korn Shell:
  463.         typeset -l l
  464.         for f in *; do
  465.         l="$f"
  466.         mv $f $l
  467.         done
  468.  
  469.       If you wanted to be really thorough and handle files with `funny'
  470.       names (embedded blanks or whatever) you'd need to use
  471.  
  472.     Bourne Shell:
  473.  
  474.         for f in *; do
  475.           g=`expr "xxx$f" : 'xxx\(.*\)' | tr '[A-Z]' '[a-z]'`
  476.           mv "$f" "$g"
  477.         done
  478.  
  479.       The `expr' command will always print the filename, even if it
  480.       equals `-n' or if it contains a System V escape sequence like `\c'.
  481.  
  482.       Some versions of "tr" require the [ and ], some don't.  It
  483.       happens to be harmless to include them in this particular
  484.       example; versions of tr that don't want the [] will conveniently
  485.       think they are supposed to translate '[' to '[' and ']' to ']'.
  486.  
  487.       If you have the "perl" language installed, you may find this
  488.       rename script by Larry Wall very useful.  It can be used to
  489.       accomplish a wide variety of filename changes.
  490.  
  491.     #!/usr/bin/perl
  492.     #
  493.     # rename script examples from lwall:
  494.     #       rename 's/\.orig$//' *.orig
  495.     #       rename 'y/A-Z/a-z/ unless /^Make/' *
  496.     #       rename '$_ .= ".bad"' *.f
  497.     #       rename 'print "$_: "; s/foo/bar/ if <stdin> =~ /^y/i' *
  498.  
  499.     $op = shift;
  500.     for (@ARGV) {
  501.         $was = $_;
  502.         eval $op;
  503.         die $@ if $@;
  504.         rename($was,$_) unless $was eq $_;
  505.     }
  506.  
  507. ------------------------------
  508.  
  509. Subject: Why do I get [some strange error message] when I "rsh host command" ?
  510. Date: Thu Mar 18 17:16:55 EST 1993
  511.  
  512. 2.7)  Why do I get [some strange error message] when I "rsh host command" ?
  513.  
  514.       (We're talking about the remote shell program "rsh" or sometimes
  515.       "remsh" or "remote"; on some machines, there is a restricted shell
  516.       called "rsh", which is a different thing.)
  517.  
  518.       If your remote account uses the C shell, the remote host will
  519.       fire up a C shell to execute 'command' for you, and that shell
  520.       will read your remote .cshrc file.  Perhaps your .cshrc contains
  521.       a "stty", "biff" or some other command that isn't appropriate for
  522.       a non-interactive shell.  The unexpected output or error message
  523.       from these commands can screw up your rsh in odd ways.
  524.  
  525.       Here's an example.  Suppose you have
  526.  
  527.     stty erase ^H
  528.     biff y
  529.  
  530.       in your .cshrc file.  You'll get some odd messages like this.
  531.  
  532.     % rsh some-machine date
  533.     stty: : Can't assign requested address
  534.     Where are you?
  535.     Tue Oct  1 09:24:45 EST 1991
  536.  
  537.       You might also get similar errors when running certain "at" or
  538.       "cron" jobs that also read your .cshrc file.
  539.  
  540.       Fortunately, the fix is simple.  There are, quite possibly, a
  541.       whole *bunch* of operations in your ".cshrc" (e.g., "set
  542.       history=N") that are simply not worth doing except in interactive
  543.       shells.  What you do is surround them in your ".cshrc" with:
  544.  
  545.         if ( $?prompt ) then
  546.             operations....
  547.         endif
  548.  
  549.       and, since in a non-interactive shell "prompt" won't be set, the
  550.       operations in question will only be done in interactive shells.
  551.  
  552.       You may also wish to move some commands to your .login file; if
  553.       those commands only need to be done when a login session starts
  554.       up (checking for new mail, unread news and so on) it's better to
  555.       have them in the .login file.
  556.  
  557. ------------------------------
  558.  
  559. Subject: How do I ... and have that change affect my current shell?
  560. Date: Thu Mar 18 17:16:55 EST 1993
  561.  
  562. 2.8)  How do I {set an environment variable, change directory} inside
  563.       a program or shell script and have that change affect my
  564.       current shell?
  565.  
  566.       In general, you can't, at least not without making special
  567.       arrangements.  When a child process is created, it inherits a
  568.       copy of its parent's variables (and current directory).  The
  569.       child can change these values all it wants but the changes won't
  570.       affect the parent shell, since the child is changing a copy of
  571.       the original data.
  572.  
  573.       Some special arrangements are possible.  Your child process could
  574.       write out the changed variables, if the parent was prepared to
  575.       read the output and interpret it as commands to set its own
  576.       variables.
  577.  
  578.       Also, shells can arrange to run other shell scripts in the
  579.       context of the current shell, rather than in a child process, so
  580.       that changes will affect the original shell.
  581.  
  582.       For instance, if you have a C shell script named "myscript":
  583.  
  584.     cd /very/long/path
  585.     setenv PATH /something:/something-else
  586.  
  587.       or the equivalent Bourne or Korn shell script
  588.  
  589.     cd /very/long/path
  590.     PATH=/something:/something-else export PATH
  591.  
  592.       and try to run "myscript" from your shell, your shell will fork
  593.       and run the shell script in a subprocess.  The subprocess is also
  594.       running the shell; when it sees the "cd" command it changes *its*
  595.       current directory, and when it sees the "setenv" command it
  596.       changes *its* environment, but neither has any effect on the
  597.       current directory of the shell at which you're typing (your login
  598.       shell, let's say).
  599.  
  600.       In order to get your login shell to execute the script (without
  601.       forking) you have to use the "." command (for the Bourne or Korn
  602.       shells) or the "source" command (for the C shell).  I.e. you type
  603.  
  604.     . myscript
  605.  
  606.       to the Bourne or Korn shells, or
  607.  
  608.     source myscript
  609.  
  610.       to the C shell.
  611.  
  612.       If all you are trying to do is change directory or set an
  613.       environment variable, it will probably be simpler to use a C
  614.       shell alias or Bourne/Korn shell function.  See the "how do I get
  615.       the current directory into my prompt" section of this article for
  616.       some examples.
  617.  
  618. ------------------------------
  619.  
  620. Subject: How do I redirect stdout and stderr separately in csh?
  621. From: msb@sq.com (Mark Brader)
  622. Date: Mon, 26 Oct 1992 20:15:00 -0500
  623.  
  624. 2.9)  How do I redirect stdout and stderr separately in csh?
  625.  
  626.       In csh, you can redirect stdout with ">", or stdout and stderr
  627.       together with ">&" but there is no direct way to redirect stderr
  628.       only.  The best you can do is
  629.  
  630.     ( command >stdout_file ) >&stderr_file
  631.  
  632.       which runs "command" in a subshell;  stdout is redirected inside
  633.       the subshell to stdout_file, and both stdout and stderr from the
  634.       subshell are redirected to stderr_file, but by this point stdout
  635.       has already been redirected so only stderr actually winds up in
  636.       stderr_file.
  637.  
  638.       If what you want is to avoid redirecting stdout at all, let sh
  639.       do it for you.
  640.  
  641.     sh -c 'command 2>stderr_file'
  642.  
  643. ------------------------------
  644.  
  645. Subject: How do I tell inside .cshrc if I'm a login shell?
  646. Date: Thu Mar 18 17:16:55 EST 1993
  647.  
  648. 2.10) How do I tell inside .cshrc if I'm a login shell?
  649.  
  650.       When people ask this, they usually mean either
  651.  
  652.     How can I tell if it's an interactive shell?  or
  653.     How can I tell if it's a top-level shell?
  654.  
  655.       You could perhaps determine if your shell truly is a login shell
  656.       (i.e. is going to source ".login" after it is done with ".cshrc")
  657.       by fooling around with "ps" and "$$".  Login shells generally
  658.       have names that begin with a '-'.  If you're really interested in
  659.       the other two questions, here's one way you can organize your
  660.       .cshrc to find out.
  661.  
  662.     if (! $?CSHLEVEL) then
  663.         #
  664.         # This is a "top-level" shell,
  665.         # perhaps a login shell, perhaps a shell started up by
  666.         # 'rsh machine some-command'
  667.         # This is where we should set PATH and anything else we
  668.         # want to apply to every one of our shells.
  669.         #
  670.         setenv      CSHLEVEL        0
  671.         set home = ~username        # just to be sure
  672.         source ~/.env               # environment stuff we always want
  673.     else
  674.         #
  675.         # This shell is a child of one of our other shells so
  676.         # we don't need to set all the environment variables again.
  677.         #
  678.         set tmp = $CSHLEVEL
  679.         @ tmp++
  680.         setenv      CSHLEVEL        $tmp
  681.     endif
  682.  
  683.     # Exit from .cshrc if not interactive, e.g. under rsh
  684.     if (! $?prompt) exit
  685.  
  686.     # Here we could set the prompt or aliases that would be useful
  687.     # for interactive shells only.
  688.  
  689.     source ~/.aliases
  690.  
  691. ------------------------------
  692.  
  693. Subject: How do I construct a ... matches all files except "." and ".." ?
  694. Date: Thu Mar 18 17:16:55 EST 1993
  695.  
  696. 2.11) How do I construct a shell glob-pattern that matches all files
  697.       except "." and ".." ?
  698.  
  699.       You'd think this would be easy.
  700.  
  701.       *        Matches all files that don't begin with a ".";
  702.  
  703.       .*         Matches all files that do begin with a ".", but
  704.          this includes the special entries "." and "..",
  705.          which often you don't want;
  706.  
  707.       .[!.]*   (Newer shells only; some shells use a "^" instead of
  708.          the "!"; POSIX shells must accept the "!", but may
  709.          accept a "^" as well; all portable applications shall
  710.          not use an unquoted "^" immediately following the "[")
  711.  
  712.          Matches all files that begin with a "." and are
  713.          followed by a non-"."; unfortunately this will miss
  714.          "..foo";
  715.  
  716.       .??*     Matches files that begin with a "." and which are
  717.          at least 3 characters long.  This neatly avoids
  718.          "." and "..", but also misses ".a" .
  719.  
  720.       So to match all files except "." and ".." safely you have to use
  721.       3 patterns (if you don't have filenames like ".a" you can leave
  722.       out the first):
  723.  
  724.     .[!.]* .??* *
  725.  
  726.       Alternatively you could employ an external program or two and use
  727.       backquote substitution.  This is pretty good:
  728.  
  729.       `ls -a | sed -e '/^\.$/d' -e '/^\.\.$/d'`
  730.  
  731.     (or `ls -A` in some Unix versions)
  732.  
  733.       but even it will mess up on files with newlines, IFS characters
  734.       or wildcards in their names.
  735.  
  736. ------------------------------
  737.  
  738. Subject: How do I find the last argument in a Bourne shell script?
  739. Date: Thu Mar 18 17:16:55 EST 1993
  740.  
  741. 2.12) How do I find the last argument in a Bourne shell script?
  742.  
  743.       Answer by:
  744.     Martin Weitzel <@mikros.systemware.de:martin@mwtech.uucp>
  745.     Maarten Litmaath <maart@nat.vu.nl>
  746.  
  747.       If you are sure the number of arguments is at most 9, you can use:
  748.  
  749.     eval last=\${$#}
  750.  
  751.       In POSIX-compatible shells it works for ANY number of arguments.
  752.       The following works always too:
  753.  
  754.     for last
  755.     do
  756.         :
  757.     done
  758.  
  759.       This can be generalized as follows:
  760.  
  761.     for i
  762.     do
  763.         third_last=$second_last
  764.         second_last=$last
  765.         last=$i
  766.     done
  767.  
  768.       Now suppose you want to REMOVE the last argument from the list,
  769.       or REVERSE the argument list, or ACCESS the N-th argument
  770.       directly, whatever N may be.  Here is a basis of how to do it,
  771.       using only built-in shell constructs, without creating subprocesses:
  772.  
  773.     t0= u0= rest='1 2 3 4 5 6 7 8 9' argv=
  774.  
  775.     for h in '' $rest
  776.     do
  777.         for t in "$t0" $rest
  778.         do
  779.             for u in $u0 $rest
  780.             do
  781.                 case $# in
  782.                 0)
  783.                     break 3
  784.                 esac
  785.                 eval argv$h$t$u=\$1
  786.                 argv="$argv \"\$argv$h$t$u\""    # (1)
  787.                 shift
  788.             done
  789.             u0=0
  790.         done
  791.         t0=0
  792.     done
  793.  
  794.     # now restore the arguments
  795.     eval set x "$argv"                    # (2)
  796.     shift
  797.  
  798.       This example works for the first 999 arguments.  Enough?
  799.       Take a good look at the lines marked (1) and (2) and convince
  800.       yourself that the original arguments are restored indeed, no
  801.       matter what funny characters they contain!
  802.  
  803.       To find the N-th argument now you can use this:
  804.  
  805.     eval argN=\$argv$N
  806.  
  807.       To reverse the arguments the line marked (1) must be changed to:
  808.  
  809.     argv="\"\$argv$h$t$u\" $argv"
  810.  
  811.       How to remove the last argument is left as an exercise.
  812.  
  813.       If you allow subprocesses as well, possibly executing nonbuilt-in
  814.       commands, the `argvN' variables can be set up more easily:
  815.  
  816.     N=1
  817.  
  818.     for i
  819.     do
  820.         eval argv$N=\$i
  821.         N=`expr $N + 1`
  822.     done
  823.  
  824.       To reverse the arguments there is still a simpler method, that
  825.       even does not create subprocesses.  This approach can also be
  826.       taken if you want to delete e.g. the last argument, but in that
  827.       case you cannot refer directly to the N-th argument any more,
  828.       because the `argvN' variables are set up in reverse order:
  829.  
  830.     argv=
  831.  
  832.     for i
  833.     do
  834.         eval argv$#=\$i
  835.         argv="\"\$argv$#\" $argv"
  836.         shift
  837.     done
  838.  
  839.     eval set x "$argv"
  840.     shift
  841.  
  842. ------------------------------
  843.  
  844. Subject: What's wrong with having '.' in your $PATH ?
  845. Date: Thu Mar 18 17:16:55 EST 1993
  846.  
  847. 2.13) What's wrong with having '.' in your $PATH ?
  848.  
  849.       A bit of background: the PATH environment variable is a list of
  850.       directories separated by colons.  When you type a command name
  851.       without giving an explicit path (e.g. you type "ls", rather than
  852.       "/bin/ls") your shell searches each directory in the PATH list in
  853.       order, looking for an executable file by that name, and the shell
  854.       will run the first matching program it finds.
  855.  
  856.       One of the directories in the PATH list can be the current
  857.       directory "." .  It is also permissible to use an empty directory
  858.       name in the PATH list to indicate the current directory.  Both of
  859.       these are equivalent
  860.  
  861.       for csh users:
  862.  
  863.     setenv PATH :/usr/ucb:/bin:/usr/bin
  864.     setenv PATH .:/usr/ucb:/bin:/usr/bin
  865.  
  866.       for sh or ksh users
  867.  
  868.     PATH=:/usr/ucb:/bin:/usr/bin export PATH
  869.     PATH=.:/usr/ucb:/bin:/usr/bin export PATH
  870.  
  871.       Having "." somewhere in the PATH is convenient - you can type
  872.       "a.out" instead of "./a.out" to run programs in the current
  873.       directory.  But there's a catch.
  874.  
  875.       Consider what happens in the case  where "." is the first entry
  876.       in the PATH.  Suppose your current directory is a publically-
  877.       writable one, such as "/tmp".  If there just happens to be a
  878.       program named "/tmp/ls" left there by some other user, and you
  879.       type "ls" (intending, of course, to run the normal "/bin/ls"
  880.       program), your shell will instead run "./ls", the other user's
  881.       program.  Needless to say, the results of running an unknown
  882.       program like this might surprise you.
  883.  
  884.       It's slightly better to have "." at the end of the PATH:
  885.  
  886.     setenv PATH /usr/ucb:/bin:/usr/bin:.
  887.  
  888.       Now if you're in /tmp and you type "ls", the shell will
  889.       search /usr/ucb, /bin and /usr/bin for a program named
  890.       "ls" before it gets around to looking in ".", and there
  891.       is less risk of inadvertently running some other user's
  892.       "ls" program.  This isn't 100% secure though - if you're
  893.       a clumsy typist and some day type "sl -l" instead of "ls -l",
  894.       you run the risk of running "./sl", if there is one.
  895.       Some "clever" programmer could anticipate common typing
  896.       mistakes and leave programs by those names scattered
  897.       throughout public directories.  Beware.
  898.  
  899.       Many seasoned Unix users get by just fine without having
  900.       "." in the PATH at all:
  901.  
  902.     setenv PATH /usr/ucb:/bin:/usr/bin
  903.  
  904.       If you do this, you'll need to type "./program" instead
  905.       of "program" to run programs in the current directory, but
  906.       the increase in security is probably worth it.
  907.  
  908. ------------------------------
  909.  
  910. End of unix/faq Digest part 2 of 7
  911. **********************************
  912.  
  913. -- 
  914. Ted Timar - tmatimar@isgtec.com
  915.  
  916.